﻿using System.Data;
using MySql.Data.MySqlClient;
using StructureMap;
using StructureMap.Graph;
using StructureMap.Pipeline;

/*
 * the structuremap version is 2.6.3,if using big than 3,it will can't complie
 */
namespace ConsoleApp.Dapper
{
    public sealed class StructureMapIoc
    {
        public static IContainer Initialize()
        {
            //ObjectFactory.Initialize(x =>
            //{
            //    x.Scan(scan =>
            //    {
            //        scan.WithDefaultConventions();
            //    });

            //    x.For<IDbConnection>().HybridHttpOrThreadLocalScoped().Use(c => new MySqlConnection("Server=127.0.0.1;Port=3306;Database=bbtest;Uid=root;Pwd=root;charset=utf8;"));
            //});
            //return ObjectFactory.Container;

            /// The static ObjectFactory wrapper for Container is still available in 3.0, 
            /// but we strongly recommend against using it for new applications. 
            /// It only exists for easier compatibility with older installations.

            var container = new Container(x =>
            {
                x.Scan(scan =>
                {
                    scan.WithDefaultConventions();
                });

                x.For<IDbConnection>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.ThreadLocal))
                    .Use(c => new MySqlConnection("Server=127.0.0.1;Port=3306;Database=bbtest;Uid=root;Pwd=root;charset=utf8;"));
            });

            return container;
        }
    }
}
